﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmSocketTest
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.btnTest.Click += btnTest_Click;
            this.btnRemove.Click += btnRemove_Click;
            EventHelper.ModelData += EventHelper_ModelData; ;
        }

        private void EventHelper_ModelData(Domain.Entities.OperModel t)
        {
            if (this.IsHandleCreated && !this.IsDisposed)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    lbxCommand.Items.Add(string.Format("测试成功{0}{1}{2}！", t.x, t.Operation, t.y));
                }));
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (this.IsHandleCreated && !this.IsDisposed)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    lbxCommand.Items.Clear();
                }));
            }
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            Test();
        }

        public void Test()
        {
            string method = MethodBase.GetCurrentMethod().Name;
            Domain.Entities.OperModel operModel = new Domain.Entities.OperModel { x = 1, y = 2, Operation = "Add" };
            var functionResponsBytes =
                CommHelper.NetworkDataHelper.GetNetworkDataHelper.GetRequestBytes(method, operModel);
            SocketClient.GetSocketClient.SendAsyncData(functionResponsBytes);
        }
    }
}